<?php

// +----------------------------------------------------------------------
// | Apprh.Shop
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2027 http://www.apprh.com All rights reserved.
// +----------------------------------------------------------------------
// | Notice: This code is not open source, it is strictly prohibited
// |         to distribute the copy, otherwise it will pursue its
// |         legal responsibility.
// +----------------------------------------------------------------------

// +----------------------------------------------------------------------

namespace app\home\controller;

use service\LogService;
use service\NodeService;
use think\Db;
use think\Request;
use think\Validate;
use think\Controller;
use app\common\tools\Arr;

/**
 * 注册
 * Class Index
 * @package app\home\controller
 * @author pp <124910168@qq.com>
 * @date 2020/08/15 10:41
 */
class Register extends Controller
{

    public $user;

    /**
     * 控制器基础方法
     */
    public function _initialize()
    {
        // halt(session('member'));
        parent::_initialize();
        $this->user = Db::connect(config('dbx.SU'))->name('Member');
    }

    public function index()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            // halt($post);
            $username = trim($post['username']);
            $password = trim($post['password']);
            $repassword = trim($post['repassword']);
            if (strlen($username) < 6) {
                return ['status' => 0, 'msg' => '账号长度不能少于6位有效字符', 'url' => url('@home/register')];
            }
            if (strlen($password) < 6) {
                return ['status' => 0, 'msg' => '密码长度不能少于6位有效字符', 'url' => url('@home/register')];
            }
            if (strlen($repassword) < 6) {
                return ['status' => 0, 'msg' => '密码长度不能少于6位有效字符', 'url' => url('@home/register')];
            }
            if ($password != $repassword) {
                return ['status' => 0, 'msg' => '密码不一致', 'url' => url('@home/register')];
            }

            // 验证码
            $verifyCode = $this->request->post('code', '', 'trim');
            if( !captcha_check($verifyCode) ) {
                $this->error('验证码不正确');
            }
            
            // 用户信息验证
            $hasUser = $this->user->where(['username|phone|mail' => $username])->find();
            if ($hasUser) {
                return ['status' => 0, 'msg' => '账号已存在，请重新输入', 'url' => url('@home/register')];
            }
            
            $data = [
                'username' => $username,
                'password' => md5($password),
                'create_at' =>time(),
            ];
            $int = $this->user->insertGetId($data);
            $user = $this->user->find($int);
            // halt($user);
            if($int){
                session('uid', $user['id']);
                session('username', $user['username']);
                session('member', $user);
                return ['status' => 1, 'msg' => '注册成功，正在进入系统', 'url' => url('@home/member.index/index')];
            }
        }else{
            return $this->fetch('index', ['title' => '用户注册']);
        }
    }

    public function verify_code(){
		ob_clean();
        if (is_mobile()){
            $array = array(
                'imageW' => 80,
                'imageH' => 39,
                'length' => 4,
                'fontSize'=>12,
                // 是否画混淆曲线
                'useNoise' => false,
                'useCurve' => false,
            );
        }else{
            $array = array(
                'imageW' => 130,
                'imageH' => 38,
                'length' => 4,
                'fontSize'=>16,
                // 是否画混淆曲线
                'useNoise' => false,
                'useCurve' => false,
            );
        }


        return captcha("", $array);
    }

    //绑定用户
    public function bind(){
        if(request()->isPost()){
            // halt(request()->post());
            if(empty(input('username'))){
                $this->error("请填写后台账号或手机号码");
            }
            $user = $this->user->where(['username|phone' => trim(input('username'))])->find();
            // halt($user);
            if(!$user){
                $this->error("找不到账号");                
            }
            $weixUserInfo = session ("wx_userinfo");
            $data = array(
                'wxopenid' => $weixUserInfo['wxopenid'],
                'wxunionid' => $weixUserInfo['wxunionid'],
                'avatar' => $weixUserInfo['avatar'],
            );
            Db::name('Member')->where(['id' => $user['id']])->update($data);
            session('uid', $user['id']);
            session('username', $user['username']);
            session('member', $user);
            // $this->success("恭喜您绑定成功", url('home/member.index/index'));
            $this->redirect(url('home/member.index/index'));
        }else{
            return $this->fetch();
        }
    }
}
